package net.jjjshop.front.controller.lbt;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.entity.lbt.BalanceResponse;
import net.jjjshop.common.entity.lbt.UserExtract;
import net.jjjshop.common.entity.lbt.UserExtractEntity;
import net.jjjshop.common.entity.lbt.UserExtractSearchRequest;
import net.jjjshop.framework.common.api.ApiResult;
import net.jjjshop.framework.log.annotation.OperationLog;
import net.jjjshop.front.controller.BaseController;
import net.jjjshop.front.param.user.UserExtractRequest;
import net.jjjshop.front.service.lbt.LbUserExtractService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@Api(value = "UserExtractController", tags = {"提现"})
@RestController
@RequestMapping("/shop/userExtract")
public class UserExtractController  extends BaseController {

    @Autowired
    private LbUserExtractService userExtractService;
    /**
     * 提现申请
     */
    @ApiOperation(value = "提现申请")
    @RequestMapping(value = "/extractApply", method = RequestMethod.POST)
    public ApiResult<String> extractApply(@RequestBody @Validated UserExtractRequest request) {
        Boolean apply = userExtractService.extractApply(request);
        if (apply){
            return ApiResult.ok(null,"申请成功");
        }else{
            return ApiResult.fail("申请失败");
        }
    }


    /**
     * 分页显示用户提现表
     * @param request 搜索条件
     * @param
     */
    //@PreAuthorize("hasAuthority('admin:finance:apply:list')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ApiResult<IPage<UserExtractEntity>>  getList(@Validated UserExtractSearchRequest request, Integer page, Integer limit){
        return ApiResult.ok(userExtractService.getList(request, page,limit));
    }

    /**
     * 修改用户提现表
     * @param id integer id
     * @param userExtractRequest 修改参数
     */
    //@PreAuthorize("hasAuthority('admin:finance:apply:update')")
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResult<String> update(@RequestParam Integer id, @Validated UserExtractRequest userExtractRequest){
        if (userExtractService.updateExtract(id, userExtractRequest)) {
            return ApiResult.ok("更新成功");
        } else {
            return ApiResult.fail("失败");
        }

    }

    /**
     * 提现统计
     * @Param dateLimit 时间限制 today,yesterday,lately7,lately30,month,year,/yyyy-MM-dd hh:mm:ss,yyyy-MM-dd hh:mm:ss/
     */
    //@PreAuthorize("hasAuthority('admin:finance:apply:balance')")
    @ApiOperation(value = "提现统计")
    @RequestMapping(value = "/balance", method = RequestMethod.POST)
    public ApiResult<BalanceResponse> balance(@RequestParam(value = "dateLimit", required = false,defaultValue = "")
                                                      String dateLimit){
        return ApiResult.ok(userExtractService.getBalance(dateLimit));
    }

    /**
     * 提现审核
     * @param id    提现id
     * @param status    审核状态 -1 未通过 0 审核中 1 已提现
     * @param backMessage   驳回原因
     * @return 审核结果
     */
    //@PreAuthorize("hasAuthority('admin:finance:apply:apply')")
    @ApiOperation(value = "提现申请审核")
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public ApiResult<String> updateStatus(@RequestParam(value = "id") Integer id,
                                          @RequestParam(value = "status",defaultValue = "审核状态 -1 未通过 0 审核中 1 已提现") Integer status,
                                          @RequestParam(value = "backMessage",defaultValue = "驳回原因", required = false) String backMessage){
        if(userExtractService.updateStatus(id, status, backMessage)){
            return ApiResult.ok("更新成功");
        }else{
            return ApiResult.fail("更新失败");
        }
    }


    @RequestMapping(value = "/export", method = RequestMethod.GET)
    @OperationLog(name = "export")
    @ApiOperation(value = "export", response = String.class)
    public ApiResult<String> export(UserExtractSearchRequest request, HttpServletResponse httpServletResponse) throws Exception{
        userExtractService.extractExport(request, httpServletResponse);
        return ApiResult.ok(null, "订单导出成功");
    }

}
